home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 September / macformat-004.iso / Shareware City / Graphics / VideoToolbox ƒ / Demos / Grating.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-07  |  6.4 KB  |  177 lines  |  [TEXT/KAHL]

  1. /*
  2. Grating.c
  3. This demo shows how to load the clut and put a vignetted grating onto the
  4. screen. It also saves the image to disk as a PICT file. This demo has been kept
  5. as simple as possible, to enhance readability, forsaking accurate control of
  6. time and contrast of the stimulus. The FlickeringGrating demo, on the other
  7. hand, is somewhat more elaborate and presents a research-grade stimulus if the
  8. LuminanceRecord.h calibration data are accurate.
  9.  
  10. For a real experiment there are some refinements you should consider. This demo
  11. takes a while to create the image on the screen, but you probably want
  12. frame-accurate timing in your experiment. You could either create the image in
  13. an offscreen GWorld and copy it to the screen with CopyBitsQuickly. Or you could
  14. set the clut to uniform gray (every clut entry equal to the background
  15. luminance) while you're computing the image on-screen, and then load a grayscale
  16. ramp into the clut when you want the display to start. Similarly you can make
  17. the image disappear by loading a new image, calling EraseRect(), or loading 
  18. a uniform-gray clut.
  19.  
  20. Another issue that matters for a real experiment is gamma correction. The
  21. numbers loaded into the clut are faithfully transformed into voltages by the
  22. three digital-to-analog converters on your video card, but the resulting
  23. luminance produced on your monitor will be an approximately parabolic function
  24. of the video voltage. Apple provides crude gamma correction by means of a
  25. generic 8-bit gamma table in the video driver, which does make things look
  26. better, but is not accurately matched to the gamma of your particular monitor,
  27. which depends on the current settings of its brightness and contrast knobs.
  28. Furthermore the Apple 8-bit gamma-correction scheme, which simply transforms the
  29. nominal 8-bit clut value into a new 8-bit clut value, necessarily restricts the
  30. range of available values, mapping several onto one output value, and omitting
  31. some output values. In other words your luminances will be 8-bit quantized
  32. twice, both before and after gamma correction. It is preferable to do gamma
  33. correction first, without quantization. Therefore I suggest you eliminate
  34. Apple's gamma correction, by calling GDUncorrectedGamma(), and use the
  35. Luminance.c package to do gamma correction. That package implements the
  36. published algorithm of Pelli and Zhang (1991). The FlickeringGrating demo uses
  37. Luminance.c.
  38.  
  39. Why not merge main() and Grating()? The call to Require() checks for any needed
  40. hardware. It is vital that a test for the presence of a needed floating point
  41. unit be done before entering any routine, e.g. Grating(), that uses the fpu,
  42. because the THINK C compiler typically produces code that accesses the fpu, to
  43. save fpu registers, at the beginning of the routine, so any subsequent check
  44. would be too late.
  45.  
  46. HISTORY:
  47. 2/7/93    dgp wrote it, as an answer to questions from Bill Merigan and David Brainard.
  48. 2/18/93    dgp    added fpu test.
  49. 2/23/93    dgp    use new GDOpenWindow1 and GDDisposeWindow1.
  50. 4/18/93    dgp    support directType.
  51. 7/7/93    dgp    prefer screen 1. Added ScrollRect for compatibility with Radius PowerView.
  52. 10/2/93    dgp    added SAVE_PICT to test PixMapToPICT().
  53. 4/27/94 dgp now ask user whether to save grating as PICT.
  54. */
  55. #include "VideoToolbox.h"
  56. #include <math.h>
  57. #if THINK_C
  58.     #include <console.h>
  59. #endif
  60. #if mc68881    // use explicit fpu instructions for highest possible speed
  61.     #define exp _exp
  62.     #define sin _sin
  63. #endif
  64. void Grating(void);
  65. #define SIZE 300
  66.  
  67. void main(void)
  68. {
  69.     Require(gestalt8BitQD);
  70.     Grating();
  71. }
  72.  
  73. void Grating(void)
  74. {
  75.     short i,j,error,clutSize,pixelSize,mode;
  76.     short preferredPixelSize[6]={8,32,16,4,2,1};    // Order of preference
  77.     GDHandle device;
  78.     static ColorSpec table[256];
  79.     WindowPtr window,oldPort,w;
  80.     Rect r;
  81.     double a,fX[SIZE],fY[SIZE];
  82.     char string[100];
  83.     Point pt={100,0};
  84.     RgnHandle rgn;
  85.     int savePict;
  86.     
  87.     StackGrow(10000);
  88.     #if THINK_C
  89.         console_options.top = 0;
  90.         console_options.left = 0;
  91.         console_options.nrows = 4;
  92.         console_options.ncols = 60;
  93.         printf("\n");
  94.     #else
  95.         InitGraf((Ptr) &thePort);
  96.         InitFonts();
  97.         InitWindows();
  98.         InitCursor();
  99.     #endif
  100.     printf("Welcome to Grating.\n");
  101.     GetPort(&oldPort);
  102.     device=GetScreenDevice(1);
  103.     if(device==NULL)device=GetScreenDevice(0);
  104.     
  105.     // Try to get the best possible pixelSize.
  106.     pixelSize=(**(**device).gdPMap).pixelSize;
  107.     if(pixelSize!=preferredPixelSize[i] && NewPaletteManager()){
  108.         for(i=0;i<sizeof(preferredPixelSize)/sizeof(short);i++){
  109.             if(pixelSize==preferredPixelSize[i])break;
  110.             mode=HasDepth(device,preferredPixelSize[i],0,0);
  111.             if(mode!=0){
  112.                 printf("Changing pixelSize to %d bits.\n",preferredPixelSize[i]);
  113.                 error=SetDepth(device,mode,0,0);
  114.                 break;
  115.             }
  116.         }
  117.         pixelSize=(**(**device).gdPMap).pixelSize;
  118.     }
  119.  
  120.     printf("Save grating to disk as a PICT file?");
  121.     savePict=YesOrNo(1);
  122.     printf("\n");
  123.     
  124.     // Load the clut with a grayscale ramp.
  125.     GDSaveGamma(device);
  126.     GDUncorrectedGamma(device);    // Tell the driver to faithfully copy our colors into 
  127.                                 // the clut without any transformation.
  128.     clutSize=GDClutSize(device);
  129.     for(i=0;i<clutSize;i++){
  130.         table[i].rgb.red=table[i].rgb.green=table[i].rgb.blue=i*(long)0xffff/(clutSize-1);
  131.     }
  132.     error=GDSetEntriesByType(device,0,clutSize-1,table);
  133.  
  134.     // Open window and put grating in it.
  135.     SetMouse(pt);
  136.     window=GDOpenWindow1(device);
  137.     SetPort(window);
  138.     PmBackColor((clutSize-1)/2);    // This works because GDOpenWindow1 marked
  139.     EraseRect(&window->portRect);    // all the colors as pmExplicit.
  140.     SetRect(&r,0,0,SIZE,SIZE);
  141.     CenterRectInRect(&r,&window->portRect);
  142.     for(i=0;i<SIZE;i++){
  143.         a=(i-SIZE/2)/(SIZE/6.);
  144.         fY[i]=exp(-a*a);
  145.         fX[i]=fY[i]*sin((i-SIZE/2)*(2.0*PI/80.0));
  146.     }
  147.     for(j=0;j<SIZE;j++){
  148.         unsigned long row[SIZE];
  149.         for(i=0;i<SIZE;i++) row[i]=0.5+(clutSize-1)*0.5*(1.0+fY[j]*fX[i]);
  150.         if(pixelSize==16)for(i=0;i<SIZE;i++) row[i]*=1+(1<<5)+(1<<10);
  151.         if(pixelSize==32)for(i=0;i<SIZE;i++) row[i]*=1+(1<<8)+(1UL<<16);
  152.         SetPixelsQuickly(r.left,r.top+j,row,SIZE);
  153.     }
  154.     #if 0
  155.         // Force the Radius PowerView
  156.         // to update the screen from the video buffer in memory.
  157.         // It works, but I don't actually want to scroll.
  158.         // A better solution would be to call ShieldCursor/ShowCursor.
  159.         rgn=NewRgn();
  160.         ScrollRect(&r,0,1,rgn);
  161.         ScrollRect(&r,0,-1,rgn);
  162.         DisposeRgn(rgn);
  163.     #endif
  164.     if(savePict){
  165.         PixMapToPICT("grating.pict",((CWindowPtr)window)->portPixMap
  166.             ,&r,2,NULL);
  167.         printf("Image was saved to disk as file “grating.pict”.\n");
  168.     }
  169.     printf("Done. Hit return to quit.\n");
  170.     gets(string);
  171.     SetPort((WindowPtr)oldPort);
  172.     GDDisposeWindow1(window);
  173.     GDRestoreGamma(device);
  174.     GDRestoreDeviceClut(device);
  175.     abort();
  176. }
  177.